package Chuong06;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Bai01 extends JFrame implements Runnable{

	Thread tLine,tOval;
	JPanel canvas = new JPanel();
	JButton
		b1 = new JButton("StartLine"), 
		b2 = new JButton("SuspendLine"),
		b3 = new JButton("ResumeLine"),
		b4 = new JButton("StopLine"),
		b5 = new JButton("StartOval"), 
		b6 = new JButton("SuspendOval"),
		b7 = new JButton("ResumeOval"),
		b8 = new JButton("StopOval");
	JTextField t1 = new JTextField(15), t2 = new JTextField(15);
	
	public Bai01(){
		super("Bài 01 - Chương 06");
		
		JPanel panelButtonLine = new JPanel();
		panelButtonLine.setLayout(new FlowLayout());
		panelButtonLine.add(b1);
		panelButtonLine.add(b2);
		panelButtonLine.add(b3);
		panelButtonLine.add(b4);
		panelButtonLine.add(t1);
		
		JPanel panelButtonOval = new JPanel();
		panelButtonOval.setLayout(new FlowLayout());
		panelButtonOval.add(b5);
		panelButtonOval.add(b6);
		panelButtonOval.add(b7);
		panelButtonOval.add(b8);
		panelButtonOval.add(t2);
		
		add(panelButtonLine, BorderLayout.NORTH);
		
		add(canvas, BorderLayout.CENTER);
		canvas.setBackground(Color.YELLOW);
		
		add(panelButtonOval, BorderLayout.SOUTH);
		
		setSize(new Dimension(800,400));
		
		b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				tLine.start();
				t1.setText("Line is runing...");
			}			
		});
		
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tLine.suspend();
				t1.setText("Line is pause!");
			}
		}
		);
		b3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tLine.resume();
				t1.setText("Line is runing...");
			}
		}
		);
		b4.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tLine.stop();
				t1.setText("Line is stop!");
			}
		}
		);
		
		b5.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				tOval.start();
				t2.setText("Oval is runing...");
			}			
		});
		
		b6.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tOval.suspend();
				t2.setText("Oval is pause!");
			}
		}
		);
		b7.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tOval.resume();
				t2.setText("Oval is runing...");
			}
		}
		);
		b8.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tOval.stop();
				t2.setText("Oval is stop!");
			}
		}
		);
		
		tLine = new Bai01_Line(canvas);
		tOval = new Bai01_Oval(canvas);
	}
	
	public static void main(String[] args) {
		Bai01 frm = new Bai01();
		frm.setVisible(true);

	}

	@Override
	public void run() {
		int width = getSize().width;
		int height = getSize().height;
		while(true){
			Graphics g = canvas.getGraphics();
			Color c = new Color((int)(255.0*Math.random()),
								(int)(255.0*Math.random()),
								(int)(255.0*Math.random()));
			g.setColor(c);
			
			
		}		
	}

}


